package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiCreatePlotResp;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_SON_HAI_EXPLORE, desc = "山海伏兽-探索")
public class CrossSonHaiExploreCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiExploreReqMsg reqMsg = SonHaiProto.SonHaiExploreReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiExploreRespMsg.Builder respMsg = SonHaiProto.SonHaiExploreRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_EXPLORE, respMsg));
            return;
        }
        //探索
        try {
            //探索
            SonHaiCreatePlotResp resp = activity.explore(packet.getPlayerId(), reqMsg.getX(), reqMsg.getY(), reqMsg.getEventId());
            //获取玩家
            CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
            //构造PB
            SonHaiProto.SonHaiExploreResult.Builder resultBuilder = SonHaiActivityPb.buildSonHaiExploreResult(resp);
            SonHaiProto.SonHaiUserDataSimple.Builder userBuilder = SonHaiActivityPb.buildSonHaiUserDataSimple(user, false);
            respMsg.setRet(0);
            respMsg.setExploreResult(resultBuilder);
            respMsg.setUserData(userBuilder);
            respMsg.setActivityId(reqMsg.getActivityId());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_SON_HAI_EXPLORE_FROM_CROSS, respMsg));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_EXPLORE, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
